package org.jeecg.modules.flowable.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.modules.flowable.common.dic.ResultMessage;
import org.jeecg.modules.flowable.common.util.json.ObjectConvert;
import org.jeecg.modules.flowable.controller.dto.ActActionResult;
import org.jeecg.modules.flowable.dic.DictKey;
import org.jeecg.modules.flowable.entity.TActActionEntity;
import org.jeecg.modules.flowable.mapper.ExtFlowableMapper;
import org.jeecg.modules.flowable.service.ActionManagerService;
import org.jeecg.modules.flowable.service.bo.ActAction;
import org.jeecg.modules.flowable.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Title:  <br>
 * Desc:  <br>
 * Date: 6/14/22 1:21 PM <br>
 * Company: 联通（辽宁）产业互联网有限公司 <br>
 *
 * @author zxd
 * @version 1.0.0
 */
@Api(value = "ActionManagerController", tags = {"(module-flowable)-动作管理"})
@ApiSort(value = 1)
@RestController
@RequestMapping("/ActionManagerController")
@Slf4j
public class ActionManagerController {

    @Autowired
    private ActionManagerService actionManagerService;

    @Autowired
    private ExtFlowableMapper extFlowableMapper;

    /**
     * 根据动作类型查询动作列表
     *
     * @return
     */
    @RequiresRoles({"admin"})
    @ApiOperation(value = "根据动作类型查询动作列表", notes = "根据动作类型查询动作列表")
    @RequestMapping(path = "/queryActionList", method = RequestMethod.POST)
    @ResponseBody
    public Result<List<ActActionResult>> queryActionList(String argAtionType) {
        List<TActActionEntity> tActActionEntities = actionManagerService.queryActionList(argAtionType);
        List<Map<String, Object>> maps = extFlowableMapper.queryDictItem(DictKey.FLOWABLE_ACTION_TYPE);
        Map<Integer, String> collect = maps.stream().collect(Collectors.toMap(k -> {
            return Integer.valueOf(k.get("dictValue").toString());
        }, v -> {
            return v.get("dictLabel").toString();
        }));

        List<ActActionResult> list = new ArrayList<>();
        for (TActActionEntity tActActionEntity : tActActionEntities) {
            ActActionResult convert = ObjectConvert.convert(tActActionEntity, ActActionResult.class);
            String s = collect.get(tActActionEntity.getActionType());
            convert.setActionTypeName(s);
            list.add(convert);
        }
        return Result.OK(list);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "动作保存", notes = "动作保存")
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result<TActActionEntity> save(ActAction argActAction) {
        TActActionEntity save = actionManagerService.save(argActAction);
        return Result.OK(save);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "跟据ID删除动作", notes = "跟据ID删除动作")
    @RequestMapping(path = "/deleteAction", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> deleteAction(String argId) {
        actionManagerService.deleteAction(argId);
        return Result.OK(ResultMessage.OK);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "保存表单设计", notes = "跟据ID删除动作")
    @RequestMapping(path = "/saveFormDesigner", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> saveFormDesigner(String argId, String argContent) {
        actionManagerService.saveDesigner(argId, argContent);
        return Result.OK(ResultMessage.OK);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "查询动作类型", notes = "查询动作类型")
    @RequestMapping(path = "/queryActionType", method = RequestMethod.POST)
    @ResponseBody
    public Result<List<Map<String, Object>>> queryActionType() {
        List<Map<String, Object>> maps = extFlowableMapper.queryDictItem(DictKey.FLOWABLE_ACTION_TYPE);
        return Result.OK(maps);
    }
}